<?php


namespace app\index\controller;


use think\Controller;
use app\index\model\User as UserModel;

/**
 * 用户controller
 * Class User
 * @package app\index\controller
 */
class User extends Controller
{
    /**
     * 查询单条数据
     */
    public function select_one()
    {
        $res = UserModel::where('id', 10000025)
            ->find();
        dump($res);
    }

    /**
     * 查询多条数据
     */
    public function select_mul()
    {
        $res = UserModel::where('age', '>', 40)
            ->select();
        dump($res);
    }

    /**
     * 保存数据
     */
    public function save1()
    {
        $user = new UserModel();
        $user->name = '古力娜扎';
        $user->age = 20;
        $res = $user->save();
        dump($res);
    }

    /**
     * 保存数据
     */
    public function save2()
    {
        $user = new UserModel();
        $res = $user->save([
            'name' => '迪丽热巴',
            'age' => 25
        ]);
        dump($res);
    }

    /**
     * 保存数据
     */
    public function save3()
    {
        $user = new UserModel([
            'name' => '安迪',
            'age' => 35
        ]);
        $res = $user->save();
        dump($res);
    }

    /**
     * 查找并更新
     */
    public function update1()
    {
        $user = UserModel::get(10000036);
        $user->age = 23;
        $res = $user->isAutoWriteTimestamp(true)->save();
        dump($res);
    }

    /**
     * 查找并更新
     */
    public function update2()
    {
        $user = UserModel::where('status', 1)
            ->where('name', '迪丽热巴')
            ->find();
        $user->age = 26;
        $res = $user->save();
        dump($res);
    }

    /**
     * 静态方法更新
     */
    public function update3()
    {
        $res = UserModel::where('id', 10000036)
            ->update(['name' => '迪丽热巴1', 'age' => 25]);
        dump($res);
    }

    /**
     * 删除
     */
    public function delete1()
    {
        try {
            $res = UserModel::get(10000038)
                ->delete();
            dump($res);
        } catch (\Exception $e) {
            dump('删除失败');
        }
    }

    /**
     * 通过主键删除
     */
    public function delete2()
    {
        try {
            $res = UserModel::destroy(10000037);
            dump($res);
        } catch (\Exception $e) {
            dump('删除失败');
        }
    }

    /**
     * 空数据
     */
    public function select_empty()
    {
        try {
            $res = UserModel::where('id', 10000036)
                ->find();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (\Exception $e) {
            return json(['msg' => '查询失败！', 'data' => $e]);
        }
    }

    /**
     * 通过主键查询多条
     */
    public function select_all()
    {
        try {
            $res = UserModel::all([10000035, 10000036]);
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (\Exception $e) {
            return json(['msg' => '查询失败！', 'data' => $e]);
        }
    }

    /**
     * 通过主键查询多条
     */
    public function get_data()
    {
        try {
            $res = UserModel::get(10000036);
            return json(['msg' => '查询成功！', 'data' => $res, 'name' => $res->getData('name'), 'all' => $res->getData()]);
        } catch (\Exception $e) {
            return json(['msg' => '查询失败！', 'data' => $e]);
        }
    }

    /**
     * withAttr处理查询结果
     */
    public function select_with_attr()
    {
        try {
            $res = UserModel::select()->withAttr('name', function ($val, $data) {
                return $val . '12';
            });
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (\Exception $e) {
            return json(['msg' => '查询失败！', 'data' => $e]);
        }
    }

    /**
     * sql：SELECT * FROM `t_user` WHERE  `name` LIKE '%巴%'
     * withAttr处理查询结果
     */
    public function select_with_search()
    {
        try {
            $res = UserModel::withSearch(['name'], [
                'name' => '巴',
                'sort' => ['name' => 'desc']
            ])->select();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (\Exception $e) {
            return json(['msg' => '查询失败！', 'data' => $e]);
        }
    }


    /**
     * 查询单条数据,追加字段
     */
    public function select_one_append()
    {
        $res = UserModel::where('id', 10000026)
            ->find();
        //$res->toArray();
        // dump($res);
        // echo json_encode($res);
        echo $res->toJson();
    }

    /**
     * 关联查询
     */
    public function select_user_orders()
    {
        $user = UserModel::find(10000025);
        $user->order;
        echo $user->order->order_name;
        //dump($user);
        // return json($user);
    }

    /**
     * 预载入查询
     */
    public function select_user_orders_all()
    {
        $user = UserModel::with('order')->select();
        return json($user);
    }

    /**
     * 关联查询
     */
    public function select_user_orders_with_join()
    {
        $user = UserModel::withJoin('order')->select();
        return json($user);
    }

    /**
     * 关联查询
     */
    public function select_user_orders_with_left_join()
    {
        $user = UserModel::field(['id', 'name', 'age'])->withJoin(['order' => ['order_name', 'price']], 'LEFT')->select();
        return json($user);
    }

}